﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Vue.Admin.Models;
using Vue.Admin.Models;
using Vue.Admin.Tools;

namespace Vue.Admin.Api.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Authorize]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EmpController : ControllerBase
    {
        private readonly DBContext _db;

        #region 注入数据库上下文
        public EmpController(DBContext db)
        {
            _db = db;
        }
        #endregion

        #region 获取人员列表
        /// <summary>
        /// 获取人员列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetEmpList()
        {

            var d = await _db.EBaseEmp.ToListAsync();
            var dto = from a in d
                      select new EmpDto
                      {
                          FUserGuid = a.FuserGuid,
                          FaddTime = a.FaddTime,
                          Femail = a.Femail,
                          Fid = a.Fid,
                          FisDelete = a.FisDelete,
                          FmodifyTime = a.FmodifyTime,
                          Fname = a.Fname,
                          Fphone = a.Fphone,
                          FuserName = a.FuserName
                      };

            return dto != null ? new Result { Code = 1, Message = "获取成功", Object = dto } : new Result { Code = 0, Message = "无数据" };

        }
        #endregion

        #region 获取单个人员的信息
        /// <summary>
        /// 获取单个人员的信息
        /// </summary>
        /// <param name="empId">人员id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetEmpInfo(int empId)
        {
            var d = await _db.EBaseEmp.FirstOrDefaultAsync(x => x.Fid == empId).ToAsyncEnumerable().ToList();
            var dto = from a in d
                      select new EmpDto
                      {
                          FUserGuid = a.FuserGuid,
                          FaddTime = a.FaddTime,
                          Femail = a.Femail,
                          Fid = a.Fid,
                          FisDelete = a.FisDelete,
                          FmodifyTime = a.FmodifyTime,
                          Fname = a.Fname,
                          Fphone = a.Fphone,
                          FuserName = a.FuserName
                      };
            return dto != null ? new Result() { Code = 1, Message = "获取成功", Object = d } : new Result() { Code = 0, Message = "无数据" };
        }
        #endregion

        #region 添加注册用户
        /// <summary>
        /// 添加注册用户
        /// </summary>
        /// <param name="dto">用户实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> RegisterEmp([FromBody] RegisterEmpDto dto)
        {
            if (!ModelState.IsValid) return new Result { Code = -1, Message = "模型验证不通过" };
            var emp = new EBaseEmp
            {
                FaddTime = DateTime.Now,
                Femail = dto.FEmail,
                FisDelete = 0,
                FmodifyTime = null,
                Fname = dto.FName,
                Fphone = dto.FPhone,
                Fpwd = MD5Tools.MD5Hash($"a123456").ToLower(),
                FuserName = dto.FUserName
            };
            await _db.EBaseEmp.AddAsync(emp);
            var num = await _db.SaveChangesAsync();
            return num > 0 ? new Result { Code = 1, Message = "注册成功" } : new Result { Code = 0, Message = "注册失败" };
        }
        #endregion

        #region 删除用户
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="empId">人员ID</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<Result> DelEmp(int empId)
        {
            //软删除
            EBaseEmp e = _db.EBaseEmp.FirstOrDefault(x => x.Fid == empId);
            if (e != null)
            {
                e.FisDelete = 1;
                _db.EBaseEmp.Update(e);
            }

            return await _db.SaveChangesAsync() > 0 ? new Result { Code = 1, Message = "删除成功" } : new Result { Code = 0, Message = "删除失败" };
        }
        #endregion
    }
}